package emas.client.handler;

import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LatLngCallback;
import com.google.gwt.maps.client.geom.LatLng;

import emas.shared.Location;

public class GeoCoderHandler {

	private static GeoCoderHandler instance = new GeoCoderHandler();

	private LatLng addressLatlng = null;
	private DataHandler dataHandler;
	private Location location;

	private GeoCoderHandler() {
	}

	public void getLatLngfromAddressForLocation(Location loc, DataHandler dataH) {
		Geocoder geocoder = new Geocoder();

		this.location = loc;
		this.dataHandler = dataH;

		geocoder.getLatLng(loc.getAddress(), new LatLngCallback() {

			@Override
			public void onFailure() {

			}

			@Override
			public void onSuccess(LatLng point) {
				addressLatlng = LatLng.newInstance(point.getLatitude(),
						point.getLongitude());
				location.setLat(addressLatlng.getLatitude());
				location.setLng(addressLatlng.getLongitude());

				dataHandler.addLocation(location);

			}
		});

	}

	public static GeoCoderHandler getInstance() {
		if (instance == null) {
			instance = new GeoCoderHandler();
		}

		return instance;
	}

}
